package au.com.langdale.gwt.client.model;

import java.util.Date;

/**
 * Base for all Date properties.  Concrete properties must 
 * provide _get(), _set(), and getName().
 * 
 * @author adv
 *
 */
public abstract class DateProperty extends Property {

	protected abstract Date _get(Object model);
	protected abstract void _set(Object model, Date value);
	
	public DateProperty() {
		// serializable ctor
	}
	
	public Date getDate(Object model) {
		return _get(model);
	}
	
	public void setDate(Object model, Date value) {
		_set(model, value);
	}

	public String get(Object model) {
		Date value = _get(model);
		if( value != null)
			return value.toString();
		else
			return "";
	}

	public void set(Object model, String value) {
		if( value.length() == 0)
			_set(model, null);
		else
			try {
				_set(model, new Date(Date.parse(value)));
			}
			catch( IllegalArgumentException ex) {
				_set(model, null);
			}
	}

	public boolean isSet(Object model) {
		return _get(model) != null;
	}

	public void set(Object model, boolean value) {
	}
	public String getStyleName() {
		return "date-field";
	}
	public int getLength() {
		return 16;
	}
}
